import datetime
import tkinter as tk
from gui.config import *
import tkinter.ttk as ttk
from gui.head_info import *
import datetime as dt


def financial_page(master, func, handle):
    title = tk.Label(master, text='财务报表系统', font=('黑体', 18, 'bold'), bg='white')\
        .pack(fill='x', side='top', pady=10)
    ttk.Separator(master, orient='horizontal').pack(side='top', fill='x')

    tree = ttk.Treeview(master, show='headings', height=500)
    tree["columns"] = ("日期", "客流量", "总收入", "单人房入住", "单人房收入", "双人房入住", "双人房收入", "大床房入住", "大床房收入")
    tree.column("日期", width=100, anchor='center')
    tree.column("客流量", width=50, anchor='center')
    tree.column("总收入", width=50, anchor='center')
    tree.column("单人房入住", width=50, anchor='center')
    tree.column("单人房收入", width=50, anchor='center')
    tree.column("双人房入住", width=50, anchor='center')
    tree.column("双人房收入", width=50, anchor='center')
    tree.column("大床房入住", width=50, anchor='center')
    tree.column("大床房收入", width=50, anchor='center')
    tree.heading("日期", text="日期")
    tree.heading("客流量", text="客流量")
    tree.heading("总收入", text="总收入")
    tree.heading("单人房入住", text="单人房入住")
    tree.heading("单人房收入", text="单人房收入")
    tree.heading("双人房入住", text="双人房入住")
    tree.heading("双人房收入", text="双人房收入")
    tree.heading("大床房入住", text="大床房入住")
    tree.heading("大床房收入", text="大床房收入")


    def look_by_day():
        date = datetime.today()

        x = tree.get_children()
        for item in x:
            tree.delete(item)

        for i in range(30):
            ret = handle.select_date_income(f"{date.year}-{date.month}-{date.day}")
            if ret[0][1] is None:
                tree.insert('', i, values=(f"{date.year}-{date.month}-{date.day}", 0, 0, 0, 0, 0, 0, 0, 0))
            else:
                tree.insert('', i, values=ret[0])
            date += dt.timedelta(days=-1)

    # 从上个月开始查询每月收入
    def look_by_month():
        date = datetime.today()
        year = date.year
        month = date.month

        x = tree.get_children()
        for item in x:
            tree.delete(item)

        for i in range(12):
            ret = handle.select_month_income(year, month)
            if ret[0][1] is None:
                tree.insert('', i, values=(f"{year}-{month}", 0, 0, 0, 0, 0, 0, 0, 0))
            else:
                tree.insert('', i, values=ret[0])
            month -= 1
            if month == 0:
                month = 12
                year -= 1

    def look_by_year():
        year = datetime.now().year
        x = tree.get_children()
        for item in x:
            tree.delete(item)

        for i in range(10):
            ret = handle.select_year_income(year)
            if ret[0][1] is None:
                tree.insert('', i, values=(f"{year}", 0, 0, 0, 0, 0, 0, 0, 0))
            else:
                tree.insert('', i, values=ret[0])
            year -= 1

    def back():
        master.destroy()
        func()

    search_line = tk.Frame(master, bg='white')

    tk.Button(search_line, text="近一个月/日", font=("楷体", 13), bd=1, command=look_by_day, bg='powderblue')\
        .pack(side='left', padx=(10, 10))
    tk.Button(search_line, text="近一年/月", font=("楷体", 13), bd=1, command=look_by_month, bg='powderblue') \
        .pack(side='left', padx=(10, 10))
    tk.Button(search_line, text="近十年/年", font=("楷体", 13), bd=1, command=look_by_year, bg='powderblue') \
        .pack(side='left', padx=(10, 10))
    tk.Button(search_line, text='返回', font=('楷体', 13), bg='lightcoral', bd=1, command=back, width=5)\
        .pack(side='right', padx=(0, 20))
    search_line.pack(fill='x', side='top', pady=5)
    ttk.Separator(master, orient='horizontal').pack(side='top', fill='x')

    tree.pack(side='top', fill='x')


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('800x600+100+100')
    head = tk.Frame(root)
    head_handle(head, '19335005')
    head.pack(fill='both', side='top')
    foot = tk.Frame(root, bg='#c1cbd7')
    foot_info(root)
    foot.pack(fill='x', anchor='s', side='bottom')
    main = tk.Frame(root, bg='white')

    def func():
        pass
    financial_page(main, func)
    main.pack(fill='both')
    root.mainloop()
